home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / win32pdhutil.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  5.2 KB  |  166 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Utilities for the win32 Performance Data Helper module
  5.  
  6. Example:
  7.   To get a single bit of data:
  8.   >>> import win32pdhutil
  9.   >>> win32pdhutil.GetPerformanceAttributes("Memory", "Available Bytes")
  10.   6053888
  11.   >>> win32pdhutil.FindPerformanceAttributesByName("python", counter="Virtual Bytes")
  12.   [22278144]
  13.   
  14.   First example returns data which is not associated with any specific instance.
  15.   
  16.   The second example reads data for a specific instance - hence the list return - 
  17.   it would return one result for each instance of Python running.
  18.  
  19.   In general, it can be tricky finding exactly the "name" of the data you wish to query.  
  20.   Although you can use <om win32pdh.EnumObjectItems>(None,None,(eg)"Memory", -1) to do this, 
  21.   the easiest way is often to simply use PerfMon to find out the names.
  22. '''
  23. import win32pdh
  24. import string
  25. import time
  26. error = win32pdh.error
  27. counter_english_map = { }
  28.  
  29. def find_pdh_counter_localized_name(english_name, machine_name = None):
  30.     if not counter_english_map:
  31.         import win32api
  32.         import win32con
  33.         counter_reg_value = win32api.RegQueryValueEx(win32con.HKEY_PERFORMANCE_DATA, 'Counter 009')
  34.         counter_list = counter_reg_value[0]
  35.         for i in range(0, len(counter_list) - 1, 2):
  36.             
  37.             try:
  38.                 counter_id = int(counter_list[i])
  39.             except ValueError:
  40.                 continue
  41.  
  42.             counter_english_map[counter_list[i + 1].lower()] = counter_id
  43.         
  44.     
  45.     return win32pdh.LookupPerfNameByIndex(machine_name, counter_english_map[english_name.lower()])
  46.  
  47.  
  48. def GetPerformanceAttributes(object, counter, instance = None, inum = -1, format = win32pdh.PDH_FMT_LONG, machine = None):
  49.     path = win32pdh.MakeCounterPath((machine, object, instance, None, inum, counter))
  50.     hq = win32pdh.OpenQuery()
  51.     
  52.     try:
  53.         hc = win32pdh.AddCounter(hq, path)
  54.         
  55.         try:
  56.             win32pdh.CollectQueryData(hq)
  57.             (type, val) = win32pdh.GetFormattedCounterValue(hc, format)
  58.             return val
  59.         finally:
  60.             win32pdh.RemoveCounter(hc)
  61.  
  62.     finally:
  63.         win32pdh.CloseQuery(hq)
  64.  
  65.  
  66.  
  67. def FindPerformanceAttributesByName(instanceName, object = None, counter = None, format = win32pdh.PDH_FMT_LONG, machine = None, bRefresh = 0):
  68.     '''Find peformance attributes by (case insensitive) instance name.
  69. \t
  70. \tGiven a process name, return a list with the requested attributes.
  71. \tMost useful for returning a tuple of PIDs given a process name.
  72. \t'''
  73.     if object is None:
  74.         object = find_pdh_counter_localized_name('Process', machine)
  75.     
  76.     if counter is None:
  77.         counter = find_pdh_counter_localized_name('ID Process', machine)
  78.     
  79.     if bRefresh:
  80.         win32pdh.EnumObjects(None, machine, 0, 1)
  81.     
  82.     instanceName = string.lower(instanceName)
  83.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, -1)
  84.     instance_dict = { }
  85.     for instance in instances:
  86.         
  87.         try:
  88.             instance_dict[instance] = instance_dict[instance] + 1
  89.         continue
  90.         except KeyError:
  91.             instance_dict[instance] = 0
  92.             continue
  93.         
  94.  
  95.     
  96.     ret = []
  97.     for instance, max_instances in instance_dict.items():
  98.         for inum in xrange(max_instances + 1):
  99.             if string.lower(instance) == instanceName:
  100.                 ret.append(GetPerformanceAttributes(object, counter, instance, inum, format, machine))
  101.                 continue
  102.             None<EXCEPTION MATCH>KeyError
  103.         
  104.     
  105.     return ret
  106.  
  107.  
  108. def ShowAllProcesses():
  109.     object = 'Process'
  110.     (items, instances) = win32pdh.EnumObjectItems(None, None, object, win32pdh.PERF_DETAIL_WIZARD)
  111.     instance_dict = { }
  112.     for instance in instances:
  113.         
  114.         try:
  115.             instance_dict[instance] = instance_dict[instance] + 1
  116.         continue
  117.         except KeyError:
  118.             instance_dict[instance] = 0
  119.             continue
  120.         
  121.  
  122.     
  123.     items = [
  124.         'ID Process'] + items[:5]
  125.     print 'Process Name', string.join(items, ',')
  126.     for instance, max_instances in instance_dict.items():
  127.         for inum in xrange(max_instances + 1):
  128.             hq = win32pdh.OpenQuery()
  129.             hcs = []
  130.             for item in items:
  131.                 path = win32pdh.MakeCounterPath((None, object, instance, None, inum, item))
  132.                 hcs.append(win32pdh.AddCounter(hq, path))
  133.             
  134.             win32pdh.CollectQueryData(hq)
  135.             time.sleep(0.01)
  136.             win32pdh.CollectQueryData(hq)
  137.             print '%-15s\t' % instance[:15],
  138.             for hc in hcs:
  139.                 (type, val) = win32pdh.GetFormattedCounterValue(hc, win32pdh.PDH_FMT_LONG)
  140.                 print '%5d' % val,
  141.                 win32pdh.RemoveCounter(hc)
  142.             
  143.             print 
  144.             win32pdh.CloseQuery(hq)
  145.         
  146.     
  147.  
  148.  
  149. def BrowseCallBackDemo(counter):
  150.     (machine, object, instance, parentInstance, index, counterName) = win32pdh.ParseCounterPath(counter)
  151.     result = GetPerformanceAttributes(object, counterName, instance, index, win32pdh.PDH_FMT_DOUBLE, machine)
  152.     print "Value of '%s' is" % counter, result
  153.     print "Added '%s' on object '%s' (machine %s), instance %s(%d)-parent of %s" % (counterName, object, machine, instance, index, parentInstance)
  154.  
  155.  
  156. def browse(callback = BrowseCallBackDemo, title = 'Python Browser', level = win32pdh.PERF_DETAIL_WIZARD):
  157.     win32pdh.BrowseCounters(None, 0, callback, level, title)
  158.  
  159. if __name__ == '__main__':
  160.     ShowAllProcesses()
  161.     print 'Virtual Bytes = ', FindPerformanceAttributesByName('python', counter = 'Virtual Bytes')
  162.     print 'Available Bytes = ', GetPerformanceAttributes('Memory', 'Available Bytes')
  163.     print 'Browsing for counters...'
  164.     browse()
  165.  
  166.